<template>
<div class="app-container">
  <div>
    <span>订单管理</span>
    <span :class="{active}">您有新的订单到了，请及时查收</span>
  </div>
  
  <el-table
    :data="orders"
    style="width: 100%"
  >
    <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="商品名称">
            <span>{{ props.row.order_info }}</span>
          </el-form-item>
          <el-form-item label="所属店铺">
            <span>{{ props.row.shop }}</span>
          </el-form-item>
          <el-form-item label="商品 ID">
            <span>{{ props.row.order_id }}</span>
          </el-form-item>
          <el-form-item label="店铺 ID">
            <span>{{ props.row.shopId }}</span>
          </el-form-item>
          <el-form-item label="商品分类">
            <span>{{ props.row.category }}</span>
          </el-form-item>
          <el-form-item label="店铺地址">
            <span>{{ props.row.address }}</span>
          </el-form-item>
          <el-form-item label="商品描述">
            <span>{{ props.row.desc }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
    <el-table-column
      label="商品 ID"
      prop="order_id">
    </el-table-column>
    <el-table-column
      label="商品名称"
      prop="name">
    </el-table-column>
    <el-table-column
      label="订单描述"
      prop="order_info">
    </el-table-column>
    <el-table-column
      label="下单时间"
      prop="create_time">
    </el-table-column>
  </el-table>
</div>
</template>

<script>
import io from 'socket.io-client'

export default {
  name: 'GoodsOrder',
  data() {
    return {
      active: true,
      orders: []
    }
  },

  mounted () {
    this.init()

    // 使用websocket监听node(后端)更新的消息
    let socket = io(this.$url.socketURL)
    socket.on('connect', () => {
      console.log( '订单收到消息' )
    })
    socket.on('updateOrder', res => {
      this.$message({
        message: '您有新的订单了，请注意查收',
        type: 'success'
      })
      this.init()
    })
  },

  methods: {
    init () {
      // 获取订单的数据
      this.$api.getOrders({}).then(res => {
        console.log( res.order )
        this.orders = res.order
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container {
    .active {
      display: none;
    }
  }

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
